import axios from 'axios';

export default{
    promise_process (promise){
        return promise.then( data => [null, data.data])
                      .catch( err => [err, null]);
    },
    
    async login_axios(username, password, type){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/users/login',
            mode        : 'cors',
            method          : 'POST',
            withCredentials: true,
            headers       :{
                "Content-Type": 'application/x-www-form-urlencoded'
            },
            data          : {
                name : username,
                password : password,
                type     : type
            }
        })
        return response;
    },

    async logout(){
        const response = await axios({
            url             : `http://127.0.0.1:5000/users/logOut`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    },

    async addAccount(account){
        const response = await axios({
            url             : `http://127.0.0.1:5000/users`,
            mode            : 'cors',
            method          : 'POST',     
            withCredentials : true,
            headers         :{
                "Content-Type": "application/json"
            },
            params          : account
        })
        return response
    },


    async deleteAccount(accountId){
        const response = await axios({
            url             : `http://127.0.0.1:5000/users/${accountId}`,
            mode            : 'cors',
            method          : 'DELETE',     
            withCredentials : true,
        })
        return response
    },

    async editAccount(account){
        const response = await axios({
            url             : `http://127.0.0.1:5000/users`,
            mode            : 'cors',
            method          : 'PUT',     
            withCredentials : true,
            headers         :{
                "Content-Type": "application/json"
            },
            params          : account
        })
        return response
    },

    async getAccountByName(name){
        const response = await axios({
            url             : `http://127.0.0.1:5000/users/${name}`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    },

    // async getAccountById(id){
    //     const response = await axios({
    //         url             : `http://127.0.0.1:5000/users/${id}`,
    //         mode            : 'cors',
    //         method          : 'GET',     
    //         withCredentials : true,
    //     })
    // }

    async getAccounts(){
        const response = await axios({
            url             : `http://127.0.0.1:5000/users`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    }

}
